package com.zykj.spb.study;

import com.battcn.swagger.annotation.EnableSwagger2Doc;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import java.util.Arrays;

@SpringBootApplication
//TODO 这个注解很重要， 如果这里没有开启 那么缓存就相当于没有激活无法进行存储
@EnableCaching
//TODO swagger 启用
@EnableSwagger2Doc

@EnableAdminServer
public class StudyApplication {
    static Logger log = LoggerFactory.getLogger(StudyApplication.class);
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(StudyApplication.class);
        springApplication.setBannerMode(Banner.Mode.OFF);
        springApplication.run(args);
        log.debug("Spring Boot 启动完成！！");
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext applicationContext) {
        return args -> {
            System.out.println("查看SPring boot 默认提供的Bean：");
            String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
//            Arrays.stream(beanDefinitionNames).forEach(System.out::println);
        } ;
    }


    /**
     * dev 环境加载
     */
    @Profile("dev")
    @Configuration
    public static class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests().anyRequest().permitAll()
                    .and().csrf().disable();
        }
    }
}

